#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
	int score = 0,isballonfloot=1;
	float width, height, gravity;
	width = 600;
	height = 400;
	gravity = 0.6;
	initgraph(width, height);

    float ball_x, ball_y, radius,ball_vy;
	ball_x = width / 4;
	radius = 10;
	ball_y = height/2;
	ball_vy = 0;

	float rect_height1,  rect_width, rect_top1, rect_left_x1;
	float rect_height2,rect_top2,rect_vx, rect_left_x2;
	rect_width = 15;
	rect_height1 = 150;
	rect_height2 = 150;
	rect_left_x1 = rect_left_x2 = width * 3 / 4;
	rect_top1 = height - rect_height1;
	rect_top2 = 0;
	rect_vx = -3;

	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if ((input == ' ') && (isballonfloot == 1))
			{
				ball_vy = -12;
			}
		}
			ball_vy = ball_vy + gravity;
			ball_y = ball_y + ball_vy;
			if (ball_y >= height - radius)
			{
				ball_vy = 0;  
				ball_y = height - radius;  
				isballonfloot = 1;
				score = 0;
			}	
			if (ball_y <=radius)
			{
				ball_vy = 0;
				ball_y = radius;
				isballonfloot = 1;
				score = 0;
			}
			rect_left_x1 = rect_left_x1 + rect_vx;
			rect_left_x2 = rect_left_x2 + rect_vx;
			if(rect_left_x1 <= 0)
			{
				rect_left_x1 = width;
				score = score + 1;
				rect_height1 = rand() % int(height / 4) + height / 4; 
				rect_vx = rand() / float(RAND_MAX) * 4 - 7; 
			}
			if (rect_left_x2 <= 0)
			{
                 rect_left_x2 = width;
				 rect_height2 = rand() % int(height / 4) + height / 4;
			}
		
			if ((rect_left_x1 <= ball_x + radius)
				&& (rect_left_x1 + rect_width >= ball_x - radius)
				&& (height - rect_height1 <= ball_y + radius))
			{
				Sleep(50); 
				score = 0; 
			}
			if ((rect_left_x2 <= ball_x + radius)
				&& (rect_left_x2 + rect_width >= ball_x - radius)
				&& (rect_height2 <= ball_y + radius))
			{
				Sleep(50); 
				score = 0;
			}
			cleardevice();  
			fillcircle(ball_x, ball_y, radius);  
			fillrectangle(rect_left_x1, height - rect_height1, rect_left_x1 + rect_width, height);
			fillrectangle(rect_left_x2,rect_top2, rect_left_x2 + rect_width, rect_height2);
			TCHAR s[20]; 
			swprintf_s(s, _T("%d"), score); 
			settextstyle(40, 0, _T("宋体")); 
			outtextxy(50, 30, s); 
			Sleep(10);
	}
	closegraph();
	return 0;
}